import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';

/// 颜色类型
enum ColorType {
  /// 填充蓝色
  fillBlue,

  /// 线性蓝色
  lineBlue,

  ///线性灰色
  lineGrey,
}

class KqTabView extends StatefulWidget {
  const KqTabView(
      {super.key,
      required this.titles,
      required this.colorTypes,
      this.fontSize,
      this.disables,
      this.bgColor = KqThemeColors.bgDialog,
      this.tabIndexBlock});
  final List<String>? titles;
  final List<ColorType> colorTypes;
  //是否允许点击 默认：允许点击
  final List<bool>? disables;
  final Color? bgColor;
  final double? fontSize;
  final Function(int index, bool disable)? tabIndexBlock;
  @override
  State<KqTabView> createState() => _KqTabViewState();
}

class _KqTabViewState extends State<KqTabView> {
  //底部横线平分的
  Widget renderAverage(String title, int index) {
    Map<String, dynamic> colors =
        ColorConfig.getSpecificColor(widget.colorTypes[index]);
    Color txtColor = colors[ColorConfig.txtKey];
    Color bgColor = colors[ColorConfig.bgKey];
    Color borderColor = colors[ColorConfig.borderKey];
    bool disable = false;
    if (widget.disables != null && widget.disables!.length > index) {
      disable = widget.disables![index];
    }
    return Opacity(
      opacity: disable ? 0.45 : 1,
      child: KqInkWell(
        onTap: (() {
          if (widget.tabIndexBlock != null) {
            widget.tabIndexBlock!(index, disable);
          }
        }),
        radius: 2.r,
        borderWidth: 1.5.r,
        borderColor: borderColor,
        backgroundColor: bgColor,
        child: Container(
          height: 72.r,
          alignment: Alignment.center,
          child: Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style:
                TextStyle(fontSize: widget.fontSize ?? 28.sp, color: txtColor),
          ),
        ),
      ),
    );
  }

  List<Widget> renderList() {
    List<Widget> views = [];
    for (var i = 0; i < widget.titles!.length; i++) {
      views.add(Expanded(child: renderAverage(widget.titles![i], i)));
      if (i + 1 != widget.titles!.length) {
        views.add(Container(
          width: 16.r,
          color: KqThemeColors.bgTransparent,
        ));
      }
    }
    return views;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal:
            KqThemeManager.instance.getConfig().formConfig.paddingHorizontal ??
                24.r,
        vertical:
            KqThemeManager.instance.getConfig().formConfig.paddingVertical ??
                16.r,
      ),
      color: widget.bgColor,
      child: Row(
        children: renderList(),
      ),
    );
  }
}

class ColorConfig {
  static const String txtKey = 'txtKey';
  static const String borderKey = 'borderKey';
  static const String bgKey = 'bgKey';

  //组合颜色根据key
  static Map<String, dynamic> getSpecificColor(ColorType type) {
    if (type == ColorType.lineBlue) {
      return getLineBlue();
    } else if (type == ColorType.lineGrey) {
      return getLineGrey();
    }
    return getFillBlue();
  }

//蓝色填充
  static Map<String, dynamic> getFillBlue() {
    return {
      bgKey: KqThemeColors.bgBlue,
      borderKey: KqThemeColors.bgBlue,
      txtKey: KqThemeColors.textWhite
    };
  }

  //线性蓝色填充
  static Map<String, dynamic> getLineBlue() {
    return {
      bgKey: KqThemeColors.bgDialog,
      borderKey: KqThemeColors.bgBlue,
      txtKey: KqThemeColors.textBlue
    };
  }

  //线性灰色填充
  static Map<String, dynamic> getLineGrey() {
    return {
      bgKey: KqThemeColors.bgWhite,
      borderKey: KqThemeColors.bgD,
      txtKey: KqThemeColors.text59
    };
  }
}
